<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="测点编码" prop="cdCode">
        <el-input
          v-model="queryParams.cdCode"
          placeholder="请输入测点编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="异常类型" prop="ycType">
        <el-select
          v-model="queryParams.ycType"
          placeholder="请选择异常类型"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in measurePointStatusList"
            :key="dict.statusValue"
            :label="dict.csStatusName"
            :value="dict.statusValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="传感器安装位置" prop="sensorInstallation">
        <el-input
          v-model="queryParams.sensorInstallation"
          placeholder="请输入传感器安装位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据时间">
        <el-date-picker
          v-model="daterangeDataTime"
          size="small"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['realData:holeAlarm:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['realData:holeAlarm:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['realData:holeAlarm:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="holeAlarmList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="测点编码"
        align="center"
        prop="cdCode"
        width="180"
      />
      <el-table-column
        label="传感器类型"
        align="center"
        prop="sensorType"
        width="140"
      />
      <el-table-column
        label="传感器安装位置"
        align="center"
        prop="sensorInstallation"
        width="180"
      />
      <el-table-column
        label="异常类型"
        align="center"
        prop="ycType"
        :formatter="cdStatusFormatter"
        width="180"
      >
        <template slot-scope="scope">
          <i
            :class="scope.row.ycType | caseStatusColorFilter"
            class="el-icon-star-on"
          ></i
          ><span :class="scope.row.ycType | caseStatusColorFilter">{{
            cdStatusFormatter(scope.row.ycType)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="异常内容"
        align="center"
        prop="ycContent"
        width="180"
      />
      <el-table-column
        label="异常发生时间"
        align="center"
        prop="ycStartTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.ycStartTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="预警等级"
        align="center"
        prop="warnLevel"
        width="180"
      >
        <template slot-scope="scope">
          <span
            :class="
              scope.row.warnLevel == '1'
                ? 'status-red'
                : scope.row.warnLevel == '2'
                ? 'status-orange'
                : scope.row.warnLevel == '3'
                ? 'status-yellow'
                : 'status-blue'
            "
          >
            {{
              scope.row.warnLevel == "1"
                ? "红"
                : scope.row.warnLevel == "2"
                ? "橙"
                : scope.row.warnLevel == "3"
                ? "黄"
                : "蓝"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="异常原因"
        align="center"
        prop="ycReason"
        width="180"
      />
      <el-table-column
        label="异常录入人员"
        align="center"
        prop="ycLrPerson"
        width="180"
      />
      <el-table-column
        label="异常结束时间"
        align="center"
        prop="ycEndTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.ycEndTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="数据时间"
        align="center"
        prop="dataTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.dataTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="140"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['realData:holeAlarm:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['realData:holeAlarm:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改长观孔测点异常数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="测点编码" prop="cdCode">
          <el-input v-model="form.cdCode" placeholder="请输入测点编码" />
        </el-form-item>

        <el-form-item label="传感器类型" prop="sensorType">
          <el-select
            v-model="form.sensorType"
            placeholder="请选择传感器类型"
            style="width:100%"
          >
            <el-option
              v-for="dict in sensorTypeList"
              :key="dict.sensorTypeCode"
              :label="dict.sensorTypeDesc"
              :value="dict.sensorTypeCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="传感器安装位置" prop="sensorInstallation">
          <el-input
            v-model="form.sensorInstallation"
            placeholder="请输入传感器安装位置"
          />
        </el-form-item>
        <el-form-item label="异常类型" prop="ycType">
          <el-select
            v-model="form.ycType"
            placeholder="请选择异常类型"
            clearable
            size="small"
            style="width:100%"
          >
            <el-option
              v-for="dict in measurePointStatusList"
              :key="dict.statusValue"
              :label="dict.csStatusName"
              :value="dict.statusValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异常内容" prop="ycContent">
          <el-input
            v-model="form.ycContent"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="异常发生时间" prop="ycStartTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.ycStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预警等级" prop="warnLevel">
          <el-select
            v-model="form.warnLevel"
            placeholder="请选择预警等级"
            style="width:100%"
          >
            <el-option label="红" value="1"> </el-option>
            <el-option label="橙" value="2"> </el-option>
            <el-option label="黄" value="3"> </el-option>
            <el-option label="蓝" value="4"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="异常原因" prop="ycReason">
          <el-input
            v-model="form.ycReason"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="异常录入人员" prop="ycLrPerson">
          <el-input
            v-model="form.ycLrPerson"
            placeholder="请输入异常录入人员"
          />
        </el-form-item>
        <el-form-item label="异常结束时间" prop="ycEndTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.ycEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数据时间" prop="dataTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.dataTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHoleAlarm,
  getHoleAlarm,
  delHoleAlarm,
  addHoleAlarm,
  updateHoleAlarm
} from "@/api/realData/holeAlarm";
import { listSensorTypeData } from "@/api/dictData/sensorTypeData";
import { listMeasurePointStatus } from "@/api/dictData/measurePointStatus";
export default {
  name: "HoleAlarm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 长观孔测点异常数据表格数据
      holeAlarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据时间时间范围
      daterangeDataTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        sensorType: null,

        sensorInstallation: null,

        ycType: null,

        ycContent: null,

        ycStartTime: null,

        warnLevel: null,

        ycReason: null,

        ycLrPerson: null,

        ycEndTime: null,

        dataTime: null
      },
      //传感器类型
      sensorTypeList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "测点编码不能为空", trigger: "blur" }
        ],
        sensorType: [
          { required: true, message: "传感器类型不能为空", trigger: "change" }
        ],
        sensorInstallation: [
          { required: true, message: "传感器安装位置不能为空", trigger: "blur" }
        ],
        ycType: [
          { required: true, message: "异常类型不能为空", trigger: "change" }
        ],
        ycContent: [
          { required: true, message: "异常发生时间不能为空", trigger: "blur" }
        ],
        ycStartTime: [
          { required: true, message: "异常发生时间不能为空", trigger: "blur" }
        ],
        warnLevel: [
          { required: true, message: "预警等级不能为空", trigger: "change" }
        ],
        ycReason: [
          { required: true, message: "异常原因不能为空", trigger: "blur" }
        ],
        ycLrPerson: [
          { required: true, message: "异常录入人员不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" }
        ],
        ycEndTime: [
          { required: true, message: "异常结束时间不能为空", trigger: "blur" }
        ],
        dataTime: [
          { required: true, message: "数据时间不能为空", trigger: "blur" }
        ]
      },
      //异常类型
      measurePointStatusList: []
    };
  },
  created() {
    this.getList();
    listSensorTypeData(this.TypeParams).then(response => {
      this.sensorTypeList = response.rows;
    });
    listMeasurePointStatus().then(response => {
      this.measurePointStatusList = response.rows;
    });
  },
  methods: {
    /** 查询长观孔测点异常数据列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDataTime && "" != this.daterangeDataTime) {
        this.queryParams.params["beginDataTime"] = this.daterangeDataTime[0];
        this.queryParams.params["endDataTime"] = this.daterangeDataTime[1];
      }
      listHoleAlarm(this.queryParams).then(response => {
        this.holeAlarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        sensorType: null,

        sensorInstallation: null,

        ycType: null,

        ycContent: null,

        ycStartTime: null,

        warnLevel: null,

        ycReason: null,

        ycLrPerson: null,

        ycEndTime: null,

        dataTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDataTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加长观孔测点异常数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getHoleAlarm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改长观孔测点异常数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHoleAlarm(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHoleAlarm(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delHoleAlarm(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
